<?php

namespace app\shop\controller;

use think\Exception;
use think\Loader;
use think\Request;
use app\shop\model\Brand as BrandModel;

class Brand extends \PurchaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $brandModel = new BrandModel();

        try {
            $data = $brandModel->where([
                'is_show' => 1,
            ])->order('sort_order')->select();

            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $brandModel = new BrandModel();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $brandModel = new BrandModel();

        $data = $request->post();

        /* @var $validate \ShopValidate */
        $validate = Loader::validate('Brand');
        if (! $validate->scene('save')->check($data)) {
            return $validate->getError();
        }

        try {
            $result = $brandModel->data($data)->allowField(true)->save();

            if (! $result) {
                $this->serverErrorTerminate('资源新增失败');
            }

//            return $this->index();
            $this->successTerminate($brandModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $brandModel = new BrandModel();

        try {
            $data = $brandModel->get($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $brandModel = new BrandModel();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $brandModel = new BrandModel();

        $data = $request->post();
        $pk = $brandModel->getPk();
        if (isset($data[$pk])) unset($data[$pk]);

        try {
            $result = $brandModel->allowField(true)->save($data, [
                $pk => $id,
            ]);
            if (! $result) $this->serverErrorTerminate('更新资源失败');
            $this->successTerminate($brandModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $brandModel = new BrandModel();
    }


    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImage()
    {
        // 获取表单上传文件
        $file = request()->file('image');

        /* @var $file \Think\File */
        $info = $file->validate([
            'size' => 1024 * 100,
            'ext' => 'jpg,jpeg,png',
        ])->move(BRAND_LOGO_SAVE_PATH);

        if ($info) {
            // 成功上传后 获取上传信息
            // 输出 jpg
//            echo $info->getExtension();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
//            echo $info->getFilename();

//            $images[] = $info->getSaveName();
            $this->successTerminate(BRAND_LOGO_URL . $info->getSaveName());
        } else {
            // 上传失败获取错误信息
            $this->serverErrorTerminate($file->getError());
        }
    }
}
